package me.zhengjie.domain;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.RegisterAddressConfig;
import me.zhengjie.dto.sqlServer.*;
import me.zhengjie.modbusTCP.LogisticsAreaTCP;
import me.zhengjie.repository.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class PressingService {

    private final PinlaminationloadOneRepository pinlaminationloadOneRepository;
    private final PinlaminationloadTwoRepository pinlaminationloadTwoRepository;
    private final PalletRepository palletRepository;
    private final ProcesscardRepository processcardRepository;
    private final PressRecipesRepository pressRecipesRepository;
    private final ProInfoService proInfoService;
    private final LogisticsAreaTCP logisticsAreaTCP;

    @Transactional
    public Integer Pressing(String palletCode, Integer pressMachineId){
        List<PinlaminationloadOne> pinlaminationloadList = pinlaminationloadOneRepository.findAll();
        if (CollectionUtils.isEmpty(pinlaminationloadList)) {
            Pallet pallet = palletRepository.findPalletByPalletCode(palletCode);
            log.info("压机作业，当前载盘信息：{}",pallet);
            Processcard processcard = processcardRepository.findProcesscardById(pallet.getProcesscardId());
            log.info("压机作业，载盘的批次号信息：{}",processcard);
            PinlaminationloadOne pinlaminationload = getPinlaminationload(palletCode, processcard);
            log.info("写入压机的信息：{}",pinlaminationload);
            String lamProgram = pinlaminationload.getLamProgram();
            PressRecipes pressRecipes = pressRecipesRepository.findByLamProgram(lamProgram);

            if (ObjectUtil.isEmpty(pressRecipes)) {
                // 没有查询到压机同步资料
                log.error("没有查询到压机同步资料，载盘号：{}，压合程式：{}",palletCode,lamProgram);
                return 1;
            }

            // 回报压合区读码成功
            if (pressMachineId==1) {
                pinlaminationloadOneRepository.flush();

                // 写入载盘架
                pinlaminationloadOneRepository.saveAndFlush(pinlaminationload);
                // 回报plc读码成功
                logisticsAreaTCP.writeWithRetry(RegisterAddressConfig.onePressingIfSuccessReadCode, 1, 5);
            } else if (pressMachineId == 2){

                PinlaminationloadTwo pinlaminationloadTwo = new PinlaminationloadTwo();
                BeanUtils.copyProperties(pinlaminationload, pinlaminationloadTwo);

                pinlaminationloadTwoRepository.flush();
                // 写入载盘架
                pinlaminationloadTwoRepository.saveAndFlush(pinlaminationloadTwo);
                // 回报plc读码成功
                logisticsAreaTCP.writeWithRetry(RegisterAddressConfig.twoPressingIfSuccessReadCode, 1, 5);
            }

            // 记录压合工艺信息
            proInfoService.startPressing(DateTime.now().toTimestamp(), palletCode);

            return 1;
        }else {
            PinlaminationloadOne pinlaminationload = pinlaminationloadList.get(0);
            String barcodeNumber = pinlaminationload.getBarcodeNumber();
            if (!barcodeNumber.equals(palletCode)) {
                pinlaminationloadOneRepository.delete(pinlaminationload);
            }
            return 0;
        }
    }

    private static PinlaminationloadOne getPinlaminationload(String palletCode, Processcard processcard) {
        PinlaminationloadOne pinlaminationload = new PinlaminationloadOne();
        pinlaminationload.setBarcodeNumber(palletCode);
        pinlaminationload.setWorkNumber(processcard.getWorkNum());
        pinlaminationload.setPitchNumber(processcard.getProcesscardCode());
        pinlaminationload.setOutLayerQty(processcard.getQty());
        pinlaminationload.setInnerSize1(processcard.getSize1());
        pinlaminationload.setInnerSize2(processcard.getSize2());
        pinlaminationload.setLamProgram(processcard.getLayerName());
        pinlaminationload.setLamTemperture(1);
        pinlaminationload.setPartChange(1);
        return pinlaminationload;
    }
}
